package com.yanqu.road.server.manger.activity.yqhotel;


import com.yanqu.road.entity.activity.yqhotel.data.YqHotelStudentData;
import com.yanqu.road.entity.activity.yqhotel.data.YqHotelUserData;
import com.yanqu.road.entity.activity.yqhotel.result.YqHotelQuickStudentResult;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.gameplayer.GamePlayer;

import com.yanqu.road.server.gameplayer.module.activity.yqhotel.YqHotelModule;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class YQHotelDebugMgr {

    private static long debugServerId = 88881;

    private static RandomHelper randomHelper = new RandomHelper();

    private static Map<Long, Map<String,Integer>> debugItem = new ConcurrentHashMap<>();

    public static void initDebug(long userId){
        debugItem.put(userId,new ConcurrentHashMap<>());
        //零界只玩家一进门就加速对应建筑的学生
        /*for(int i = 1 ; i <= 7 ;i++){
            for(int j = 1; j <= 7;j++){
                //建筑1学生进入时候加速建筑1的学习学生 加速次数
                debugItem.get(userId).put("buildingEnterWaitQuickBuilding_"+i+";"+j,100);
            }
        }*/
        //随机加速学生
        debugItem.get(userId).put("buildingQuickStudyRandom_666:",1000);
    }

    public static boolean buildingEnterWaitQuickBuilding(GamePlayer player, String paramStr){
        List<Integer> intList = StringUtils.stringToIntegerList(paramStr,";");
        int testStudyBuildingId = intList.get(0);
        int testEnterBuildingId = intList.get(1);
        long nowTime = System.currentTimeMillis();
        YqHotelModule shrekModule = player.getModule(YqHotelModule.class);
        YqHotelUserData userData = shrekModule.getUserData();
        Map<Integer, YqHotelStudentData> studentMap = new ConcurrentHashMap<>(userData.getStudentMap());
        List<Integer> studentIdDesList = new ArrayList<>(studentMap.keySet());
        List<Integer> studentIdAscList = new ArrayList<>(studentMap.keySet());
        Collections.sort(studentIdDesList);
        Collections.reverse(studentIdDesList);
        Collections.sort(studentIdAscList);
        long maxLevelUpTime = shrekModule.getBuildingLevelUpTime(testStudyBuildingId);
        for(int enterStudentId : studentIdDesList){
            YqHotelStudentData enterStudent = studentMap.get(enterStudentId);
            long studentEnterTime = shrekModule.getStudentEnterTime(enterStudent.getNo(),testEnterBuildingId);
            if(nowTime > studentEnterTime && studentEnterTime > maxLevelUpTime){
                for(int studyStudentId : studentIdAscList){
                    if(studyStudentId == enterStudentId){
                        continue;
                    }
                    YqHotelStudentData studyStudent = studentMap.get(studyStudentId);
                    int buildingId = shrekModule.whereisStudent(studentEnterTime,studyStudent);
                    //差太大的不能加速，人已经没了
                    if(buildingId == testStudyBuildingId && nowTime - studentEnterTime < 5000){
                        long studyOverTime = shrekModule.getStudentStudyOverTime(studyStudentId,buildingId);
                        if(nowTime < studyOverTime){
                            YqHotelMgr.getLogger().error("调试加速建筑{}学生id:{},加速了的时间{},刚好进入学生id:{},die:{}，他的建筑等待时间{}，加速相比当前时间差了{}",
                                    studyStudentId,studyStudentId,studyOverTime - studentEnterTime,
                                    enterStudentId,enterStudent.getDieInBuilding(),enterStudent.getWaitTime(buildingId),
                                    nowTime - studentEnterTime);
                            YqHotelQuickStudentResult result = shrekModule.quickGraduate(studentEnterTime, YqHotelMgr.getActivityTimeAcId(),studyStudentId);
                            if(result.getRet() == 0){
                                return false;
                            }else{
                                YqHotelMgr.getLogger().error("加速学生{}失败{}", studyStudentId,result.getRet());
                            }
                            return false;
                        }
                    }
                }
            }
        }
        return false;
    }

    //当前时间加速
    public static boolean buildingQuickStudyRandom(GamePlayer player,String paramStr){
        int testStudyBuildingId = randomHelper.next(1,8);
        long nowTime = System.currentTimeMillis();
        YqHotelModule shrekModule = player.getModule(YqHotelModule.class);
        YqHotelUserData userData = shrekModule.getUserData();
        Map<Integer, YqHotelStudentData> studentMap = new ConcurrentHashMap<>(userData.getStudentMap());
        List<Integer> studentIdList = new ArrayList<>(studentMap.keySet());
        Collections.sort(studentIdList);
        for(int studentId : studentIdList){
            YqHotelStudentData student = studentMap.get(studentId);
            int buildingId = shrekModule.whereisStudent(nowTime,student);
            long studentEnterTime = shrekModule.getStudentEnterTime(student.getNo(),buildingId);
            //差太大的不能加速，人已经没了
            if(buildingId == testStudyBuildingId){
                long studyStartTime = shrekModule.getStudentStartStudyTime(studentId,buildingId);
                long studyOverTime = shrekModule.getStudentStudyOverTime(studentId,buildingId);
                if(nowTime > studyStartTime && nowTime < studyOverTime){
                    YqHotelMgr.getLogger().info("随机加速建筑{}学生id:{},加速了的时间{}",
                            testStudyBuildingId,studentId,studyOverTime - studentEnterTime);
                    YqHotelQuickStudentResult result = shrekModule.quickGraduate(nowTime,YqHotelMgr.getActivityTimeAcId(),studentId);
                    if(result.getRet() == 0){
                        return false;
                    }else{
                        YqHotelMgr.getLogger().info("随机加速学生{}失败{}", studentId,result.getRet());
                    }
                    return false;
                }
            }
        }
        return false;
    }


    public static void debugTimerLogic(GamePlayer player){
        if(GameServer.getInstance().getServerId() != debugServerId)return;
        //灵界时间自动加速学生测试
        String methodParamStr = "";
        List<String> methodList = new ArrayList<>(debugItem.get(player.getUserId()).keySet());
        if(methodList.size() > 0){
            methodParamStr = methodList.get(randomHelper.next(0,methodList.size()));
        }
        if(!"".equals(methodParamStr)){
            List<String> strList = StringUtils.stringToStringList(methodParamStr,"_");
            String methodStr = strList.get(0);
            String paramStr = strList.get(1);
            try {
                Class actionClass = Class.forName("com.yanqu.road.server.manger.activity.shrek.YqHotelDebugMgr" );
                Method method = actionClass.getMethod(methodStr,new Class[]{GamePlayer.class,String.class});
                Constructor c = actionClass.getConstructor();
                Object actionObject = c.newInstance();
                Object success = method.invoke(actionObject, new Object[]{player,paramStr});
                if((Boolean) success){
                    debugItem.get(player.getUserId()).put(methodParamStr,debugItem.get(player.getUserId()).get(methodStr) - 1);
                    if(debugItem.get(player.getUserId()).get(methodParamStr) == 0){
                        debugItem.get(player.getUserId()).remove(methodParamStr);
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }else{
            YqHotelMgr.getLogger().error("测试次数跑完了");
        }
    }
}
